<template>
  <div class="all-chart">
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>统计分析</el-breadcrumb-item>
      <el-breadcrumb-item>全部药材统计</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="16" :offset="4">
          <el-input v-model="queryDate" placeholder="请输入需要查询的日期 如'7.9-7.11'" width="50%">
            <el-button slot="append" icon="el-icon-search" @click="getAllChart"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <div class="d-flex jc-center mt-5 ai-end">
        <div id="main" style="width: 800px;height:500px;"></div>
        <div class="total mb-6">
          <span class="tit">总数量：</span>{{amount}} 克<span></span>
          <br>
          <br>
          <span class="tit">总价格：</span><span>{{total}} 元</span>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
  import echarts from 'echarts'
  export default {
    data() {
      return {
        queryDate: '',
        amount: 0,
        total: 0
      }
    },
    mounted() {
      var myChart = echarts.init(document.getElementById('main'))
      var option = {
        title: {
          text: '全部药材出库情况图表'
        },
        tooltip: {},
        legend: {},
        xAxis: {
          data: []
        },
        yAxis: {},
        series: [{
          name: '',
          type: 'bar',
          data: []
        }]
      }
      myChart.setOption(option)
    },
    methods: {
      async getAllChart() {
        const { data: res } = await this.$http.get('/allchart/' + this.queryDate)
        console.log(res)
        if(res.status === 'error1') {
          this.$message.error('格式错误！')
          return
        }
        if(res.status === 'error') {
          this.$message.error('没有这段时间的出库情况！')
          return
        }
        var myChart = echarts.init(document.getElementById('main'))
        myChart.setOption(res.data)
        this.total = res.total
        res.amount.forEach(item => {
          this.amount += item
        })
        this.$message.success('查询成功！')
      }
    }
  }
</script>

<style>
</style>
